package com.hzhc.utils;

import com.hzhc.common.Constants;
import com.hzhc.dto.MemberVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: SessionMemberUtil
 * @Description:
 * @version 1.0
 * @author: duanhm
 * @Date: 2020/01/03 14:13:52
 * 
 */
public class SessionMemberUtil {
	/**
	 * 获取session
	 * 
	 * @return
	 */
	public static Boolean isLoginUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if (session.getAttribute(Constants.SESSION_MEMBER_KEY) == null) {
			return false;
		}
		return true;
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	public static HttpSession getSession(HttpServletRequest request) {
		HttpSession session = request.getSession();
		return session;
	}

	/**
	 * 设置用户信息 到session
	 * 
	 * @param request
	 */
	public static void setMember(HttpServletRequest request, MemberVo memberVo) {
		request.getSession(true).setAttribute(Constants.SESSION_MEMBER_KEY, memberVo);
	}

	/**
	 * 设置用户信息 到session
	 * 
	 * @param request
	 */
	public static MemberVo getMember(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MemberVo vo = (MemberVo) session.getAttribute(Constants.SESSION_MEMBER_KEY);
		return vo;
	}

	/**
	 * 获取用户ID
	 * 
	 * @param request
	 */
	public static Integer getMemberId(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MemberVo vo = (MemberVo) session.getAttribute(Constants.SESSION_MEMBER_KEY);
		return vo.getId();
	}

	/**
	 * 删除Session值
	 * 
	 * @param request
	 */
	public static void removeMember(HttpServletRequest request) {
		request.getSession(true).removeAttribute(Constants.SESSION_MEMBER_KEY);
	}

	/**
	 * 删除Session值
	 * 
	 * @param request
	 * @param key
	 */
	public static void removeSession(HttpServletRequest request, String key) {
		request.getSession(true).removeAttribute(key);
	}
}
